#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int a[N];
int n;
int st, ed;
map<int, int>mp;
struct node
{
	int a, b;
}S[N];
int cmp(node A, node B)
{
	return A.a < B.a;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		LL cnt = 0;
		/*memset(a, 0, sizeof(a));
		memset(S, 0, sizeof(S));*/
		for (int i = 1; i <= n; i++) {
			a[i] = 0;
			S[i].a = 0;
			S[i].b = 0;
		}
		LL res = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			int x;
			scanf("%d", &x);
			cnt += x;
			S[i] = { a[i],x };
			res += (LL)x * a[i];
		}
		LL ans = 0;
		sort(S + 1, S + n + 1, cmp);
		if (cnt & 1) {
			LL ans1 = 0;
			bool flag1 = 0;
			LL idx = 0;
			for (int i = 1; i <= n; i++) {
				ans1 += S[i].b;
				if (ans1 <= cnt / 2) {
					ans += (LL)S[i].a * S[i].b;
				}
				else {
					ans1 -= S[i].b;
					LL k = cnt / 2 - ans1;
					ans += (LL)(cnt / 2 - ans1) * S[i].a;
					if ((LL)S[i].b - k > 0) {
						flag1 = 1;
						idx = i;
					}
					break;
				}
			}
			if (flag1) {
				printf("%lld\n", res -  ans-ans - S[idx].a);
				//cout << res - 2 * ans - S[idx].a << endl;
			}
			else {
				printf("%lld\n", res - ans-ans - S[idx + 1].a);
				//cout << res - 2 * ans - S[idx + 1].a << endl;
			}
		}
		else {
			LL ans1 = 0;
			for (int i = 1; i <= n; i++) {
				ans1 += S[i].b;
				if (ans1 <= cnt / 2) {
					ans += (LL)S[i].a * S[i].b;
				}
				else {
					ans1 -= S[i].b;
					LL k = cnt / 2 - ans1;
					ans += (LL)(cnt / 2 - ans1) * S[i].a;
					break;
				}
			}
			printf("%lld\n", res -  ans-ans);
			//cout << res - 2 * ans << endl;
		}
	}
	return 0;
}